var path = require('path')
  , fs = require('fs')
module.exports = function(server,fm){
  server.get('/mobile/app',function(req,res,next){
    res.locals = {
      Application:{
        assetsVersion:'lastest'
      },
      'request':{
        'locale':'zh_CN',
        'webApplicationContext':{
          servletContext:{
            servletContextName:'fed-test'
          },
          'startupDate':Date.now()
        }
      },
      'user':{
        'id':1,
        'name':'blueware',
        'payStatus':'PAID'
      },
      websiteDomain:'oneapm.com',
      staticPath:'http://oneapmfed.io/dev',
      isSaas:"true"
    }
    fm.render('mobile/applist.ftl', res.locals, function(html) {
      res.end(html)
    });
  });
  server.get('/mobile/app/setup',function(req,res,next){
    res.locals = {
      Application:{
        assetsVersion:'lastest'
      },
      request:{
        locale:'zh_CN',
        webApplicationContext:{
          servletContext:{
            servletContextName:'fed-test'
          },
          startupDate:Date.now()
        }
      },
      user:{
        name:'blueware',
        id:1,
        payStatus:'PAID'
      },
      websiteDomain:'oneapm.com',
      staticPath:'http://oneapmfed.io/dev',
      isSaas:"true",
      app:{
        id:1,
        parentId:0,
        attributes:{
          accName:'aaa'
        }
      },
      account:{
        token:'aaaaaa',
        name:'aaa',
        attributes:{
          plat:'Android'
        }
      },
      can_add:true,
      constant:{
        IOS_AGENT_DOWNLOAD_URL:'1',
        ANDROID_AGENT_DOWNLOAD_URL:'2',
        ANDROID_ECLIPSE_DOWNLOAD_URL_GT_4_4:'3',
        ANDROID_ECLIPSE_DOWNLOAD_URL_LT_4_4:'',
        ANDROID_GRADLE_DOWNLOAD_URL:'',
        ANDROID_MAVEN_DOWNLOAD_URL:''
      }
    }
    var filePath = 'mobile/setup.ftl'
    if(fs.existsSync(path.join(process.cwd(),'views',filePath))){
      fm.render(filePath,res.locals,function(html){
        res.end(html)
      })
    }else{
      next()
    }
  })
  //specs
  server.get('/mobile/app/:appId/:moduleName',function(req,res,next){
    res.locals = {
      Application:{
        assetsVersion:'lastest'
      },
      request:{
        locale:'zh_CN',
        webApplicationContext:{
          servletContext:{
            servletContextName:'fed-test'
          },
          startupDate:Date.now()
        }
      },
      user:{
        name:'blueware',
        id:1,
        payStatus:'PAID'
      },
      websiteDomain:'oneapm.com',
      staticPath:'http://oneapmfed.io/dev',
      isSaas:"true",
      app:{
        id:1,
        parentId:0,
        attributes:{
          accName:'aaa'
        }
      },
      account:{
        attributes:{
          plat:'Android'
        },
        token: 'asdfghjkl',
        id: 1234
      }
    }
    var filePath = 'mobile/'+req.params.moduleName+'.ftl'
    if(fs.existsSync(path.join(process.cwd(),'views',filePath))){
      fm.render(filePath,res.locals,function(html){
        res.end(html)
      })
    }else{
      next()
    }
  })
}
